//
//  YanZhengMaVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/19.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "YanZhengMaVC.h"
#import "WYBase64Fun.h"
#import "GongXiView.h"
#import "NSString+MD5.h"
#import "AESTool.h"

//获取验证码
#define YZM_URL @"/api/ApiUser/sendVerifyCode"
//提交用户注册信息
#define COMMIT_URL @"/api/ApiUser/registerMember"
//用户注册加密接口
#define ReisterUrl_Encrypt @"/api/ApiUser/registerMemberEncrypt"

//加密混淆字符串
#define EncryptString @"gyt^$&^*vjfGJ$$&*MV"

//注册统计
#define REG_TONGJI_URL @"register"

@interface YanZhengMaVC ()
{
    __weak IBOutlet UIButton *yzmBtn;
    __weak IBOutlet UITextField *yzmLab;
    __weak IBOutlet UIButton *commitBtn;
    __weak IBOutlet UILabel *phoneNumLab;
    __weak IBOutlet UILabel *tishiLab1;
    //定时器
    NSTimer *timer;
    int seconds;
}
@end

@implementation YanZhengMaVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //默认时间60秒
    seconds = 60;
    yzmBtn.layer.masksToBounds = YES;
    yzmBtn.layer.cornerRadius = 2;
    commitBtn.layer.masksToBounds = YES;
    commitBtn.layer.cornerRadius = 2;
    phoneNumLab.hidden = YES;
    phoneNumLab.text = self.phone;
    tishiLab1.hidden = YES;
    //屏蔽键盘事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeKeyBoard)];
    [self.view addGestureRecognizer:tap];
}
//关闭键盘
- (void)closeKeyBoard
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
//返回按钮的监听事件
- (IBAction)backBtnOnClick:(id)sender {
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

//获取验证码
- (IBAction)yzmBtnOnClick:(id)sender {
    yzmBtn.backgroundColor = [UIColor grayColor];
    [yzmBtn setTitle:@"60秒后重试" forState:UIControlStateNormal];
    yzmBtn.userInteractionEnabled = NO;
    NSDictionary *params = @{@"account":self.phone,@"check_account_exists":@"1"};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,YZM_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        tishiLab1.hidden = NO;
        phoneNumLab.hidden = NO;
    } failedBlock:^{
        
    } needLog:NO];
    //60秒倒计时
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}
//倒计时方法验证码实现倒计时60秒，60秒后按钮变换开始的样子
-(void)timerFireMethod:(NSTimer *)theTimer {
    if (seconds == 1) {
        [theTimer invalidate];
        seconds = 60;
        [yzmBtn setTitle:@"获取验证码" forState: UIControlStateNormal];
        yzmBtn.userInteractionEnabled = YES;
        yzmBtn.backgroundColor = [UIColor redColor];
    }else{
        seconds--;
        NSString *title = [NSString stringWithFormat:@"%d秒后重试",seconds];
        [yzmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [yzmBtn setTitle:title forState:UIControlStateNormal];
    }
}

//提交用户登录信息
- (IBAction)commitBtnOnCLick:(id)sender {
    
    [self registerEncrypt];
    return;
    
    
    /*
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [WYPublic addZheZhaoToWindowWithTitle:nil];
    NSDictionary *params = nil;
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *channelId = [user objectForKey:@"TongJi_channelId"];
    NSString *unionId = [user objectForKey:@"TongJi_unionId"];
    NSString *uid = [user objectForKey:@"TongJi_uid"];
    
    if (channelId!=nil && unionId!=nil) {
        params = @{@"account":self.phone,@"password":self.pwd,@"confirm_password":self.ensure_pwd,@"verify_code":yzmLab.text,@"auto_login":@"1",@"op_union_id":unionId,@"op_channel_id":channelId};
        
    }else if (uid!=nil) {
        params = @{@"account":self.phone,@"password":self.pwd,@"confirm_password":self.ensure_pwd,@"verify_code":yzmLab.text,@"auto_login":@"1",@"op_parent_uid":uid};

    }else{
        params = @{@"account":self.phone,@"password":self.pwd,@"confirm_password":self.ensure_pwd,@"verify_code":yzmLab.text,@"auto_login":@"1"};
    }
    
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,COMMIT_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if (ret.intValue == 0) {
            //统计接口
            [self tongji];
            //保存用户信息
            NSDictionary *userinfo = dic[@"data"];
            [UserInfo writeUserInfoWithDic:userinfo];
            //退出登录界面
            [WYPublic logOrRegistSuccess];
            [[WYMainTabBarVC shareMainTaBarVC]changeSelectedWithIndex:3];
            
            //恭喜
            NSString *money = [WYPublic getUsefullStr:dic[@"data"][@"money"]];
            if ([money isEqualToString:@"1"]) {
                GongXiView *gongxi = [[GongXiView alloc]init];
                [WINDOW addSubview:gongxi];
            }
            
        }else{
            [WYPublic showMessageWithTitle:nil Msg:dic[@"msg"]];
        }
        [WYPublic removeZheZhaoToWindow];
    } failedBlock:^{
        [WYPublic removeZheZhaoToWindow];
    } needLog:NO];
     */
}

//加密注册接口
- (void)registerEncrypt
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [WYPublic addZheZhaoToWindowWithTitle:nil];
    NSMutableDictionary *params = nil;
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *channelId = [user objectForKey:@"TongJi_channelId"];
    NSString *unionId = [user objectForKey:@"TongJi_unionId"];
    NSString *uid = [user objectForKey:@"TongJi_uid"];
    
    NSString *privateKey_String = nil;
    if (uid != nil) {
        privateKey_String = [[NSString alloc] initWithFormat:@"%@%@%@%@%@%@",self.phone,self.pwd,self.ensure_pwd,yzmLab.text,@"1",uid];;
    }
    else
    {
        if (!unionId) {
            unionId = @"2";
        }
        if (!channelId) {
            channelId =  @"2";
        }
        privateKey_String = [[NSString alloc] initWithFormat:@"%@%@%@%@%@%@%@",self.phone,self.pwd,self.ensure_pwd,yzmLab.text,@"1",unionId,channelId];
    }
    NSLog(@"%@",privateKey_String);
    privateKey_String = [NSString stringWithFormat:@"%@%@%@",EncryptString,privateKey_String,EncryptString];
    NSLog(@"%@",privateKey_String);
    NSString *privateKeyString_MD5 = [privateKey_String MD5Hash];
    NSLog(@"%@",privateKeyString_MD5);
    NSString *privateKey = [privateKeyString_MD5 substringWithRange:NSMakeRange(4, 16)];
    NSLog(@"%@",privateKey);

    NSString *account_Encrypt = [AESTool encryptData:@"account" withKey:privateKey];
    NSString *accountValue_Encrypt = [AESTool encryptData:self.phone withKey:privateKey];
    NSString *password_Encrypt = [AESTool encryptData:@"password" withKey:privateKey];
    NSString *passwordValue_Encrypt = [AESTool encryptData:self.pwd withKey:privateKey];
    NSString *confirm_password_Encrypt = [AESTool encryptData:@"confirm_password" withKey:privateKey];
    NSString *confirm_passwordValue_Encrypt = [AESTool encryptData:self.ensure_pwd withKey:privateKey];
    NSString *verify_code_Encrypt = [AESTool encryptData:@"verify_code" withKey:privateKey];
    NSString *verify_codeValue_Encrypt = [AESTool encryptData:yzmLab.text withKey:privateKey];
    NSString *auto_login_Encrypt = [AESTool encryptData:@"auto_login" withKey:privateKey];
    NSString *auto_loginValue_Encrypt = [AESTool encryptData:@"1" withKey:privateKey];
    
    NSString *op_union_id_Encrypt = [AESTool encryptData:@"op_union_id" withKey:privateKey];
    
    NSString *op_union_idValue_Encrypt = nil;
    if (unionId!=nil) {
        op_union_idValue_Encrypt = [AESTool encryptData:unionId withKey:privateKey];
    }
    NSString *op_channel_id_Encrypt = [AESTool encryptData:@"op_channel_id" withKey:privateKey];
    NSString *op_channel_idValue_Encrypt = nil;
    if (channelId != nil) {
        op_channel_idValue_Encrypt = [AESTool encryptData:channelId withKey:privateKey];
    }
    NSString *op_parent_uid_Encrypt = [AESTool encryptData:@"op_parent_uid" withKey:privateKey];
    NSString *op_parent_uidValue_Encrypt = nil;
    if (uid != nil) {
        op_parent_uidValue_Encrypt = [AESTool encryptData:uid withKey:privateKey];
    }
    params = [[NSMutableDictionary alloc] init];
    [params setValue:privateKey forKey:@"private_key"];
    [params setValue:accountValue_Encrypt forKey:account_Encrypt];
    [params setValue:passwordValue_Encrypt forKey:password_Encrypt];
    [params setValue:confirm_passwordValue_Encrypt forKey:confirm_password_Encrypt];
    [params setValue:verify_codeValue_Encrypt forKey:verify_code_Encrypt];
    [params setValue:auto_loginValue_Encrypt forKey:auto_login_Encrypt];
    if (op_union_idValue_Encrypt) {
        [params setValue:op_union_idValue_Encrypt forKey:op_union_id_Encrypt];
    }
    if (op_channel_idValue_Encrypt) {
        [params setValue:op_channel_idValue_Encrypt forKey:op_channel_id_Encrypt];
    }
    if (op_parent_uidValue_Encrypt) {
        [params setValue:op_parent_uidValue_Encrypt forKey:op_parent_uid_Encrypt];
    }
    
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,ReisterUrl_Encrypt] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if (ret.intValue == 0) {
            //统计接口
            [self tongji];
            //保存用户信息
            NSDictionary *userinfo = dic[@"data"];
            [UserInfo writeUserInfoWithDic:userinfo];
            //退出登录界面
//            [WYPublic logOrRegistSuccess];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
            [WYPublic showMessageWithTitle:nil Msg:@"注册成功 登录吧"];

           
            NSInteger index = 2;
            NSString *show_shopping = [[NSUserDefaults standardUserDefaults] valueForKey:@"show_shopping"];
            if (show_shopping.integerValue == 1) {
                //显示
                index = 3;
            }
            [[WYMainTabBarVC shareMainTaBarVC] changeSelectedWithIndex:index];
            
            //恭喜
//            NSString *money = [WYPublic getUsefullStr:dic[@"data"][@"money"]];
//            if ([money isEqualToString:@"1"]) {
//                GongXiView *gongxi = [[GongXiView alloc]init];
//                [WINDOW addSubview:gongxi];
//            }
            
        }else{
            [WYPublic showMessageWithTitle:nil Msg:dic[@"msg"]];
        }
        [WYPublic removeZheZhaoToWindow];
    } failedBlock:^{
        [WYPublic removeZheZhaoToWindow];
    } needLog:NO];
}
/**************
 *用户注册统计
 *************/
- (void)tongji
{
    NSDictionary *params = @{};
    [WYRequest startPostForTongJiWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_YX_IP,REG_TONGJI_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        
    } failedBlock:^{
        
    } needLog:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [timer invalidate];
}

@end
